class Solution
{
public:
    int maximumSetSize(vector<int> &nums1, vector<int> &nums2)
    {
        unordered_set<int> set1(nums1.begin(), nums1.end());
        unordered_set<int> set2(nums2.begin(), nums2.end());

        unordered_set<int> only1;
        unordered_set<int> only2;
        unordered_set<int> both;

        for (auto &i : set1)
        {
            if (set2.count(i))
            {
                both.insert(i);
            }
            else
            {
                only1.insert(i);
            }
        }

        for (auto &i : set2)
        {
            if (!set1.count(i))
            {
                only2.insert(i);
            }
        }

        int n = nums1.size();
        int half = n / 2;
        int result = 0;

        int only1Size = only1.size();
        int only2Size = only2.size();
        int bothSize = both.size();

        result += min(half, only1Size);
        result += min(half, only2Size);

        int remain = max(n - result, 0);
        result += min(remain, bothSize);

        return result;
    }
};